<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <style type="text/css">
        body, html, #allmap {
            width: 100%;
            height: 100%;
            overflow: hidden;
            margin: 0;
            font-family: "微软雅黑";
        }
    </style>

    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script type="text/javascript"
            src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=TwBb7cqto5AwQUyfneG6swdWMnSCvqy3"></script>
    <title>添加Label</title>
</head>
<body>
<div id="allmap"></div>
</body>
</html>

<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>

<script type="text/javascript">
    // 页面加载事件
    $(function () {
        //向后端发送请求获取数据库地址名和经纬度
        $.ajax({
            url: "/map/findAddress.do", success: function (result) {

                var map = new BMapGL.Map("allmap");  //创建地图对象
                map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
                var point = new BMapGL.Point(118, 35);
                map.centerAndZoom(point, 5);         //地图中心及缩放大小
                var marker = new BMapGL.Marker(point); // 创建点

                var opts = {
                    position: point,    // 指定文本标注所在的地理位置
                    offset: new BMapGL.Size(30, -30)    //设置文本偏移量
                };

                var label;

                for (var i = 0; i < result.length; i++) {
                    point = new BMapGL.Point(result[i].longitude, result[i].latitude);//获取经纬
                    marker = new BMapGL.Marker(point); // 创建点
                    opts = {
                        position: point,    // 指定文本标注所在的地理位置
                        offset: new BMapGL.Size(10, -10)    //设置文本偏移量
                    };

                    label = new BMapGL.Label(result[i].address, opts);//为文本框赋值
                    map.addOverlay(label);//添加文本框
                    map.addOverlay(marker);//添加标记
                }

            },
        });
    })

</script>